<?php
class resizeimage {
    var $width;//实际宽度
    var $height;//实际高度
    var $resize_width;//改变后的宽度
    var $resize_height;//改变后的高度
    var $cut;//是否裁图
    var $im;//临时创建的图象
    function resizeimage($handle, $wid, $hei,$c) {
        $this->im = $handle;
        $this->resize_width = $wid;
        $this->resize_height = $hei;
        $this->cut = $c;
        $this->width = imagesx($this->im);
        $this->height = imagesy($this->im);
    }
    function resize() {
        $resize_ratio = ($this->resize_width)/($this->resize_height);//改变后的图象的比例
        $ratio = ($this->width)/($this->height);//实际图象的比例
        if(($this->cut)=="1") {//裁图
            $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
            if($ratio>=$resize_ratio) {//高度优先
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height);
            }
            else {//宽度优先
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio));
            }
        } 
        else {//不裁图
            if($ratio>=$resize_ratio) {
                $newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height);
            }
            else {
                $newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height);
            }
        }
       return $newimg;
    }
}
